<template>
  <div>
    <el-row class="mb8" >
      <div style="float: left">
        <!-- 查询框 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent>
          <el-form-item :label="title+':'" prop="paramName">
            <el-input v-model="queryParams.paramName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="float: right;height: 51px;">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['basic:param:add']">添加{{ title }}</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="!multiple" @click="handleDelete"
            v-hasPermi="['basic:param:remove']">删除</el-button>
      </div>
    </el-row>
    <el-table ref="dynamicHeightTable" :max-height="tableMaxHeight - 84" v-loading="loading" :data="list"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column :label="title" prop="paramName" />
      <el-table-column label="排序" prop="sort" />
      <el-table-column label="系统内置" align="center" prop="isSystem">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isSystem ? 'Y' : 'N'"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" :disabled="scope.row.isSystem && role !== 'administrator'" @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:param:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :disabled="scope.row.isSystem && role !== 'administrator'" @click="handleDelete(scope.row)"
            v-hasPermi="['basic:param:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="open" width="680px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" inline @submit.native.prevent>
        <div class="user-info">

          <el-form-item :label="title" prop="paramName">
            <el-input v-model.trim="form.paramName" placeholder="请输入" clearable maxlength="30" />
          </el-form-item>
          <el-form-item label="显示排序" prop="sort">
            <el-input-number v-model="form.sort" controls-position="right" :min="0" />
          </el-form-item>
          <el-form-item label="系统内置" prop="isSystem" v-if="role === 'administrator'">
            <el-switch v-model="form.isSystem" active-text="是" inactive-text="否"></el-switch>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm" @keyup.enter.native="submitForm"
          native-type="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script >
import tableMaxHeightRefresh from "@/mixins/tableMaxHeightRefresh";
import { parameterConfigList,
  addParameterType, updateParameterType, delParameterType, getParameterTypeDetail } from "@/api/basic/param"

export default {
  name: "RightPanel",
  mixins: [tableMaxHeightRefresh],
  dicts: ['sys_yes_no'],
  props: {
    paramType:{
      type: String,
      required: true,
      default : ''
    },
    title:{
      type: String,
      required: true,
      default : ''
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: false,
      // 总条数
      total: 0,
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paramName: undefined,
      },
      // 表单参数
      form: {
        paramType: this.paramType,
        paramName: undefined,
        sort: undefined,
        isSystem: false
      },
      list: []
    }
  },
  computed: {
    role() {
      return this.$store.getters.roles?.[0]
    },
    // 表单校验
    rules() {
      return {
        paramName: [
          { required: true, message: this.title+"不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  watch: {
    paramType(newVal,oldVal){
      if(newVal){
        this.queryParams.paramName = ''
        this.handleQuery()
      }
    },
    'queryParams.paramName':{
      handler(newVal,oldVal){
        if(!newVal)this.handleQuery();
      },
      immediate:true
    }
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 列表 */
    getList() {
      this.loading = true
      const params = {
        ...this.queryParams,
        paramType: this.paramType
      }
      return parameterConfigList(params).then(res => {
        this.list = res.rows.sort((a,b) => { return a.sort - b.sort })
        this.total = res.total
        this.loading = false
        return Promise.resolve(res)
      }).finally(res=>{
        this.loading = false
        return Promise.resolve(res)
      })
    },
    /** 表单重置 */
    reset() {
      this.form = {
        paramType: this.paramType,
        paramName: undefined,
        sort:undefined,
        isSystem : false
      };
      this.resetForm("form");
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.dialogTitle = "添加"+this.title;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      getParameterTypeDetail(row.paramId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.dialogTitle = "修改"+this.title;
      });
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paramId)
      this.multiple = selection.length > 0
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          const form = {
            ...this.form,
            status:0
          }
          if (form.paramId != null) {
            updateParameterType(form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addParameterType(form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete (row) {
      const paramId = row.paramId || this.ids;
      const tips = row && row.paramId ? `确认删除该${this.title}吗？` : `确认删除所选择的${this.title}吗？`;
      this.$modal.confirm(tips).then(() => {
        this.loading = true;
        return delParameterType(paramId,this.paramType);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
  }

}
</script>

<style scoped>
</style>
